<?php
/**
 * cms标签管理
 * @since   2019-11-30
 * @author  曾
 */

namespace app\admin\controller;


use app\model\CmsTags;
use app\model\CmsTagData;
use app\util\ReturnCode;
use app\util\Tools;
use think\Request;

class CmsTagsList extends Base {

    /**
     * 获取标签列表
     * @return array
     * @throws \think\exception\DbException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function index() {
        $limit = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $name = $this->request->get('name', '');

        $where = [];
        if ($name) {
            $where['name'] = ['like', "%{$name}%"];
        }
        $listObj = (new CmsTags())->where($where)->order('createTime DESC')
            ->paginate($limit, false, ['page' => $start])->toArray();
        /*统计tag文档数量*/
        $tagNames = CmsTags::where([])->column('name'); // 查询name字段所有的tagname
        if ($tagNames) {
            for( $i=0; $i<count($tagNames); $i++){
                $data = CmsTags::get(['name' => $tagNames[$i]]);
                // 根据tagname 获取tagid
                $tagid = $data->id;
                $tagNum = CmsTagData::where('tagid', $tagid)->count();
                CmsTags::update([
                    'id'   => $tagid,
                    'num' => $tagNum
                ]);
            }
        }
        /*统计tag文档数量 end*/
        return $this->buildSuccess([
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ], '获取成功');

    }

    /**
     * 新增标签
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function add() {
        $postData = $this->request->post();
        // 增加新增时间字段
        $postData['createTime'] = date('Y-m-d H:i:s', time());
        $res = new CmsTags($postData);
        // 过滤post数组中的非数据表字段数据
        $res->allowField(true)->save();
        // $res = CmsContent::create($postData);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '新增失败');
        } else {
            return $this->buildSuccess([]);
        }
    }

    /**
     * 编辑标签
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function edit() {
        $postData = $this->request->post();
        $res = new CmsTags();
        // 过滤post数组中的非数据表字段数据
        $res->allowField(true)->save($postData,['id' => $postData['id']]);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '编辑失败');
        } else {
            return $this->buildSuccess([]);
        }
    }

    /**
     * 删除标签
     * @return array
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function del() {
        $id = $this->request->get('id');
        if (!$id) {
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
        }
        CmsTags::destroy($id);
        return $this->buildSuccess([]);
    }
    /*
     * tag数量统计
     * */
    public function count() {
        $tagNames = CmsTags::where([])->column('name'); // 查询name字段所有的tagname
        if ($tagNames) {
            for( $i=0; $i<count($tagNames); $i++){
                $data = CmsTags::get(['name' => $tagNames[$i]]);
                // 根据tagname 获取tagid
                $tagid = $data->id;
                $tagNum = CmsTagData::where('tagid', $tagid)->count();
                CmsTags::update([
                    'id'   => $tagid,
                    'num' => $tagNum
                ]);
            }
        }
        return $this->buildSuccess([]);
    }
}
